//
//  zuiXinJieXiaoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/8.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "zuiXinJieXiaoViewController.h"
#import "yiYuanDuoBaoViewController.h"
#import "HttpRequest.h"
#import "zuiXinTuiJian.h"
#import "zuiXinJieXiaoTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "NSString+NSDateToNSString.h"
#import "yiYuanDuoBao.h"
#import "homeViewController.h"
#import "Macros.h"
#import "TCLYUser.h"

#import "SVProgressHUD.h"
@interface zuiXinJieXiaoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,retain)zuiXinJieXiaoTableViewCell *myCell;
@property(nonatomic,copy)NSMutableArray *dataArray;

@end

@implementation zuiXinJieXiaoViewController
@synthesize myCell,dataArray;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置背景色
    [self setBackGroundColor];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    dataArray=[NSMutableArray array];
    [self getDataFromNet];
    self.myTableView.hidden=YES;
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];

}
//设置背景色
-(void)setBackGroundColor
{
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
}
//
- (IBAction)goBack:(id)sender
{
    yiYuanDuoBao *yydb=[yiYuanDuoBao shardeManager];
    NSLog(@"yydb.whichViewToTheNext=%d",yydb.whichViewToTheNext);
    if (yydb.whichViewToTheNext==1)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[homeViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
    else
    {
//        yiYuanDuoBaoViewController*yydbvc=self.navigationController.viewControllers[1];
//        [self.navigationController popToViewController:yydbvc animated:YES];
        NSLog(@"1111");
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[yiYuanDuoBaoViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }

    }
}
//从服务器获取数据
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/publish"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"uid"]=userID;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         dataArray=[json objectForKey:@"data"];
         if (dataArray.count>0) {
             [self dismiss];
         }
//         [self.myTableView reloadData];
         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
    
}
- (void)dismiss
{
    self.myTableView.hidden=NO;
    [SVProgressHUD dismiss];
    [self.myTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *str=@"cell";
    myCell=[tableView dequeueReusableCellWithIdentifier:str];
    zuiXinTuiJian*zxtj=[zuiXinTuiJian shardeManager];
    if (myCell==nil)
    {
        myCell=[[[NSBundle mainBundle] loadNibNamed:@"zuiXinJieXiaoTableViewCell" owner:self options:nil]lastObject];
        //设置点击效果,点击后不会产生灰色
        myCell.selectionStyle=UITableViewCellSelectionStyleNone;
        //设置点击效果,点击后不会产生灰色
        myCell.selectionStyle=UITableViewCellSelectionStyleNone;
        zxtj.numString=[dataArray[indexPath.row] objectForKey:@"num"];
        zxtj.picString=[dataArray[indexPath.row] objectForKey:@"pic"];
        zxtj.timeString=[dataArray[indexPath.row] objectForKey:@"time"];
        zxtj.tnameString=[dataArray[indexPath.row] objectForKey:@"tname"];
        zxtj.uidString=[dataArray[indexPath.row] objectForKey:@"uid"];
        zxtj.unameString=[dataArray[indexPath.row] objectForKey:@"uname"];
        NSString *dateString=[NSString StringNSDateToNSString:zxtj.timeString];
        //参与次数
        myCell.involveLabel.text=[NSString stringWithFormat:@"%@人次",zxtj.numString];
        //名字
        myCell.nameLabel.text=zxtj.unameString;
        myCell.goodsNameLabel.text=zxtj.tnameString;
        myCell.idLabel.text=zxtj.uidString;
        myCell.timeLabel.text=dateString;
    }
    //图像
    NSURL* imagePath = [NSURL URLWithString:zxtj.picString];
    [myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
  
    return myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 120;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
